package com.surfilter.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.surfilter.bean.QueryResult;
import com.surfilter.changesource.LayuiPageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.surfilter.bean.VirtualIdentity;
import com.surfilter.dao.VirtualMapper;
import com.surfilter.service.VirtualService;

@Service
public class VirtualServiceImpl implements VirtualService{

	Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private VirtualMapper virtualMapper;
	
	List<VirtualIdentity> listNodes=null;
	List<VirtualIdentity> listEdges=null;
	List<VirtualIdentity> ListIdentify=null;

	@SuppressWarnings("unchecked")
	public Map<String, Object> correlationAnalysis(VirtualIdentity virtual) throws Exception {
		listNodes=new ArrayList<VirtualIdentity>();
		listEdges=new ArrayList<VirtualIdentity>();
		ListIdentify=new ArrayList<VirtualIdentity>();
		ListIdentify = virtualMapper.getIdentify();
		Set<String> accountSet=new HashSet();
		Set<String> mapvir=new HashSet<String>();

		//结果集合
		Map<String, Object> result = new HashMap<String, Object>();
		//参数集合
		Map<String, String> param = new HashMap<String, String>();
		String set_Id = (null==virtual.getSet_Id() ? null : virtual.getSet_Id().toString()); //任务ID
		String type = virtual.getVirtualtype(); //虚拟身份类型
		String content = virtual.getContent(); //虚拟身份内容
		param.put("content", content);
		param.put("set_Id", set_Id);        
		getaccount(param,accountSet,mapvir);		
		result.put("nodes", listNodes);
		result.put("edges", listEdges);
		result.put("ddd", ListIdentify);
		return result;
	}




	public void getaccount(Map<String,String> map,Set accountSet,Set mapvir){
		log.info("进入方法"+map.get("content"));

		String account=map.get("content");
		accountSet.add(account);
		VirtualIdentity viraccount=new VirtualIdentity();
		viraccount.setAccount(account);
		viraccount.setName("account");
		listNodes.add(viraccount);
		try {
			List<VirtualIdentity> lista=virtualMapper.getaccountAnalysis(map);
			for(VirtualIdentity vi:lista){
					String virtualidentity = vi.getVirtualidentity();
					String replace = virtualidentity.replace("\'", "").replace("\"", "");
					vi.setVirtualidentity(replace);
					VirtualIdentity viraccountv = new VirtualIdentity();
					viraccountv.setForm(account);
					viraccountv.setTo(replace + vi.getVirtualtype());
					viraccountv.setVirtualidentity(replace);
					viraccountv.setVirtualtype( vi.getVirtualtype());
					listEdges.add(viraccountv);
					if(!mapvir.contains(virtualidentity + vi.getVirtualtype())){
						listNodes.add(vi);
					}
					Map<String,String> mapvi=new HashMap();
					mapvi.put("content", virtualidentity);
					mapvi.put("type", vi.getVirtualtype());
					if(!mapvir.contains(virtualidentity + vi.getVirtualtype())){
					getvir(mapvi,accountSet,mapvir);
				}
			}

			log.info("出方法方法"+map.get("content"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void getvir(Map<String,String> map,Set accountSet,Set mapvir){
		try {
			mapvir.add(map.get("content")+map.get("type"));
			List<VirtualIdentity> lista=virtualMapper.getvirAnalysis(map);
			for(VirtualIdentity vi:lista){
				if(!accountSet.contains(vi.getAccount())){
					Map<String,String> maps=new HashMap();
					maps.put("content", vi.getAccount());
					getaccount(maps,accountSet,mapvir);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}


	public PageInfo<VirtualIdentity> getTableShow(int pageNum) {
		List<VirtualIdentity> list = null;
		try {
		PageHelper.startPage(pageNum, 7);
		list =  virtualMapper.getTableShow();

		PageInfo<VirtualIdentity> pageInfo = new PageInfo<VirtualIdentity>(list);
		//获取总记录数
		long total = pageInfo.getTotal();
		return pageInfo;

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

    @Override
    public VirtualIdentity getDetailInfo(String account, String virtualIdentity, String virtualtype) {
        return virtualMapper.getDetailInfo(account,virtualIdentity,virtualtype);
    }

	@Override
	public LayuiPageInfo<QueryResult> queryAccountResult(QueryResult queryResult) {
		PageHelper.startPage(queryResult.getPageNum(), queryResult.getLimit());
		List<QueryResult> list = virtualMapper.queryAccountResult(queryResult);
		LayuiPageInfo<QueryResult> pageInfo = new LayuiPageInfo<QueryResult>(list);
		return pageInfo;
	}

}
